package com.surprise.ods.aliyun;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.surprise.ods.OdsConfig;

/**
 * OSSServiceForWX.java
 * @author nick
 * @since 2015年8月17日
 */
@Service
public class OSSServiceForWX {
	@Autowired
	private OdsConfig config;
	
	public String put(String key, String name, InputStream io, long length, String contentType) {
		OSSClient client = new OSSClient(config.getEndpoint(), config.getAk(),
				config.getSk());
		ObjectMetadata meta = new ObjectMetadata();
		meta.setContentLength(length);
		meta.setContentType(contentType);
		String path = key + "/" + name;
		PutObjectResult result = client.putObject(config.getBucket(), path, io,
				meta);
		System.out.println(meta.getETag());
		System.out.println(result.getETag());
		return config.getBaseUrl() + "/" + path;
	}
	
	public String put(String key, String name, byte[] bs) {
		InputStream io = new ByteArrayInputStream(bs);
		int length = bs.length;
		String filenameExtension = name.substring(name.indexOf(".") + 1).toUpperCase();
		return put(key, name, io, length, this.contentType(filenameExtension));
	}
	
	public String put(String key, String name, byte[] bs, String filenameExtension) {
		InputStream io = new ByteArrayInputStream(bs);
		int length = bs.length;
		return put(key, name, io, length, this.contentType(filenameExtension));
	}
	
	/** 
     * Description: 判断OSS服务文件上传时文件的contentType 
     * @Version1.0 
     * @param FilenameExtension 文件后缀 
     * @return String  
     */  
     public String contentType(String filenameExtension){  
        if (filenameExtension.equals("BMP") || filenameExtension.equals("bmp")) {
        	return "image/bmp";
        }
        if (filenameExtension.equals("GIF") || filenameExtension.equals("gif")) {
        	return "image/gif";
        }
        if (filenameExtension.equals("JPEG") || filenameExtension.equals("jpeg") ||  
        		filenameExtension.equals("JPG") || filenameExtension.equals("jpg") ||     
        		filenameExtension.equals("PNG") || filenameExtension.equals("png")) { 
        	return "image/jpeg";
        }
        if (filenameExtension.equals("HTML") || filenameExtension.equals("html")) {
        	return "text/html";
        }
        if (filenameExtension.equals("TXT") || filenameExtension.equals("txt")) { 
        	return "text/plain";
        }
        if (filenameExtension.equals("VSD") || filenameExtension.equals("vsd")) {
        	return "application/vnd.visio";
        }
        if (filenameExtension.equals("PPTX") || filenameExtension.equals("pptx") ||  
        		filenameExtension.equals("PPT") || filenameExtension.equals("ppt")) {
        	return "application/vnd.ms-powerpoint";
        }
        if (filenameExtension.equals("DOCX") || filenameExtension.equals("docx")||  
        		filenameExtension.equals("DOC")|| filenameExtension.equals("doc")) {
        	return "application/msword";
        }
        if (filenameExtension.equals("XML")|| filenameExtension.equals("xml")) {
        	return "text/xml";
        }
        if (filenameExtension.equals("MP4")|| filenameExtension.equals("mp4")) {
        	return "video/mpeg4";
        }
        if (filenameExtension.equals("MOV")|| filenameExtension.equals("mov")) {
        	return "video/mpeg4";
        }
        if (filenameExtension.equals("MOVIE")|| filenameExtension.equals("movie")) {
        	return "video/x-sgi-movie";
        }
        return "text/html";  
     }  
}
